// pages/mine/makeTopic/makeTopic.js

const app = getApp()

let {
  api_MakeComment,
  api_MakeAnswer
} = require("../../api/getData.js")

Page({

  /**
   * 页面的初始数据
   */
  data: {
    images: [],
    image: "",
    title: "",
    content: "",
    answerComment: "",
    answerUsername: "",
    username : "",
    userid : "",
    answerid : "",
    answertype : 0,
    commentid: -1,
    topicid: "",
    topicInfo: [],
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    let params = JSON.parse(options.answerInfo);
    let params2 = JSON.parse(options.topicInfo);
    if(options.type==1){
      this.setData({
        username: app.globalData.userInfo.username,
        userid: app.globalData.userInfo.id,
        answerComment: params.comment,
        answerUsername: params.username,
        answerid: params.id,
        commentid: -1,
        answertype: 1,
        topicInfo: params2,
      })
    }
    if(options.type==2){
      this.setData({
        username: app.globalData.userInfo.username,
        userid: app.globalData.userInfo.id,
        answerComment: params.comment,
        answerUsername: params.username,
        answerid: params.answerId,
        commentid: params.id,
        answertype: 2,
        topicInfo: params2,
      })
    }
    if(options.type==3){
      this.setData({
        username: app.globalData.userInfo.username,
        userid: app.globalData.userInfo.id,
        answerComment: params.title,
        answerUsername: params.username,
        topicid: params.id,
        answertype: 3,
        topicInfo: params2,
      })
    }
  },

  chooseImage(e) {
    wx.chooseImage({
      sizeType: ['original', 'compressed'],  //可选择原图或压缩后的图片
      sourceType: ['album', 'camera'], //可选择性开放访问相册、相机
      success: res => {
        console.log('添加照片成功')
        const images = this.data.images.concat(res.tempFilePaths)
        this.data.image = res.tempFilePaths[0];
        // 限制最多只能留下3张照片
        this.data.images = images.length <= 3 ? images : images.slice(0, 3)
      }
    })
  },

  setTitle: function (e) {
    this.setData({
      title: e.detail.value
    })
  },

  setContent: function (e) {
    this.setData({
      content: e.detail.value
    })
  },

  handleImagePreview(e) {
    const idx = e.target.dataset.idx
    const images = this.data.images

    wx.previewImage({
      current: images[idx],
      urls: images,
    })
  },

  makeComment() {
    if(this.data.answertype==3){
      let commentData = {
        userid: this.data.userid,
        comment: this.data.content,
        topicid: this.data.topicid,
      }
      api_MakeAnswer(commentData, (res) => {
        if(res.status==200){
          wx.showToast({
            title: '发表成功',
            duration: 100000,
          })
        }
        else{
          wx.showToast({
            title: '发表失败',
            duration: 100000,
          })
        }
        var topicInfo = this.data.topicInfo;
        topicInfo.answerCnt = topicInfo.answerCnt+1;
        var dataTmp = JSON.stringify(topicInfo);
        setTimeout(function () {
          wx.redirectTo({
            url: '../../mine/question/question?topic=' + dataTmp
          })
        }, 2000) 
      })
    }else{
      let commentData = {
        userId: this.data.userid,
        commentId: this.data.commentid,
        type: this.data.answertype,
        comment: this.data.content,
        answerid: this.data.answerid,
        commentUsername:this.data.answerUsername,
      }
      api_MakeComment(commentData, (res) => {
        if (res.status == 200) {
          wx.showToast({
            title: '发表成功',
            duration: 100000,
          })
        }
        else {
          wx.showToast({
            title: '发表失败',
            duration: 100000,
          })
        }
        var topicInfo = JSON.stringify(this.data.topicInfo);
        setTimeout(function () { 
          wx.redirectTo({
            url: '../../mine/question/question?topic=' + topicInfo
          })
        }, 2000) 
      })
    }
  },

  tiaozhuan:function(){
    wx.redirectTo({
      url: '../../mine/question/question?topic=' + this.data.topicInfo
    })
  }


})